package com.yong.cad.config;

import com.yong.component.ParamInterceptor;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.yong.wsdl.CrmInterfaceImpl;

import javax.xml.ws.Endpoint;

/**
 * create by yongli on 2022-04-24 18:20
 */

@Configuration
public class CxfConfig {

    private final Logger log = LoggerFactory.getLogger(CxfConfig.class);
    @Autowired
    private Bus bus;
    @Autowired
    private ParamInterceptor paramInterceptor;


    /**
     * 作用：改变服务名的前缀名
     * 此方法被注释后：wsdl访问（默认）地址为http://127.0.0.1:8080/services/user?wsdl
     * 去掉注释后：wsdl访问地址为：http://127.0.0.1:8080/mySoap/user?wsdl
     *
     * @return
     */
    @SuppressWarnings("all")
    @Bean(name = "dispatchServlet1")
    public ServletRegistrationBean dispatcherServlet1() {
        return new ServletRegistrationBean(new CXFServlet(), "/DomInterface/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public CrmInterfaceImpl domInterface() {
        return new CrmInterfaceImpl();
    }


    /**
     * JAX-WS：EndpointImpl
     * 站点服务：终端路径
     *
     * @return
     */
    @Bean(name = "endpoint1")
    public Endpoint endpoint1() {
        EndpointImpl endpointImpl = new EndpointImpl(bus, domInterface());
        endpointImpl.publish("/interface");
        endpointImpl.getInInterceptors().add(paramInterceptor);
        return endpointImpl;
    }
}

